/**
 * Created by yunpeng on 2015/4/21.
 */
var Table = require('../utils/Table').Table;
//var Count = require('../models/Count').Count;
var later = require('later');
later.date.localTime();
/*var sched = later.parse.recur().every(5).second();*/
/*var basic = {h: [13], m: [35]};//每天早上2点15
 var schedule = {schedules:[basic]};*/
var cron = later.parse.cron("10 2 * * * ");//每天早上两点10分
//var occurrences = later.schedule(cron).next(10);


later.setInterval(function(){
    var datecurr = new Date();
    var starttime = datecurr.getFullYear()+'-'+(datecurr.getMonth()+1)+'-'+(datecurr.getDate()-1)+' 00:00:00';
    var endtime = datecurr.getFullYear()+'-'+(datecurr.getMonth()+1)+'-'+datecurr.getDate()+' 00:00:00';

    new Table().getConnection(function(connection) {

        connection.beginTransaction(function (err) {
            if (err) {
                throw err;
            }
        });
        connection.query("SELECT count(status) as count FROM information WHERE  createtime>='"+starttime+"' and createtime<'"+endtime+"' and  status=1", function (err, state1) {
            if (err) {
                connection.rollback(function() {
                    throw err;
                });
            }else{
                connection.query("SELECT count(status) as count FROM information WHERE  createtime>='"+starttime+"' and createtime<'"+endtime+"' and  status=2",function(err,state2) {
                    if (err) {
                        connection.rollback(function () {
                            throw err;
                        });
                    }else{
                        connection.query("SELECT count(status) as count FROM information WHERE  createtime>='"+starttime+"' and createtime<'"+endtime+"' and  status=5",function(err,state3) {
                            if (err) {
                                connection.rollback(function () {
                                    throw err;
                                });
                            }else{
                                //var count = new count(new Date(), state1[0].count, state2[0].count, state3[0].count);
                                var count = {
                                    date:starttime,
                                    state1:state1[0].count,
                                    state2:state2[0].count,
                                    state3:state3[0].count
                                }
                                connection.query("insert into count set ?",count,function(err,data) {
                                    if (err) {
                                        connection.rollback(function () {
                                            throw err;
                                        });
                                    }
                                    connection.commit(function (err) {
                                        if (err) {
                                            connection.rollback(function () {
                                                throw err;
                                            });
                                        } else {
                                            connection.release(); //release

                                        }
                                    });
                                })
                            }

                        })
                    }

                })
            }

        });
    });
/*
    new Count(new Date(),321,12,32).save(function(result){
        console.log('sssssssssssssss');
    });*/
},cron);

function count(date, state1, state2, state3) {
        this.date = date;
        this.state1 = state1;
        this.state2 = state2;
        this.state3 = state3;

}
